package com.keboom.show.dto;

import com.keboom.show.model.SpManager;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Collections;

/**
 * getAuthorities方法将用户权限放入集合。roles应为一个list集合，这些集合中包含该账户权限，类似["goods:get","roles:delete","users:get"]
 * Created by keboom on 2020/9/8.
 */
public class SpUserDetails implements UserDetails {
    private SpManager manager;
    private String roles;

    public SpUserDetails(SpManager manager, String roles) {
        this.manager = manager;
        this.roles = roles;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限，其实roles应为一个list集合，通过循环遍历放入权限
        return Collections.singleton(new SimpleGrantedAuthority(roles));
    }

    @Override
    public String getPassword() {
        return manager.getMgPwd();
    }

    @Override
    public String getUsername() {
        return manager.getMgName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return manager.getMgState().equals(0);
    }

}
